//
//  WXAnimationManager.m
//  ParentsCommunity
//
//  Created by tianlong on 2018/7/5.
//  Copyright © 2018年 XES. All rights reserved.
//

#import "WXAnimationManager.h"

#define AniamtionManager [WXAnimationManager defaultManager]

@interface WXAnimationManager ()
@property (nonatomic, strong) NSMutableArray *animates;
@end

@implementation WXAnimationManager

#pragma mark - 全局管理
static WXAnimationManager *_instance;
+ (instancetype)defaultManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (_instance == nil) {
            _instance = [[WXAnimationManager alloc] init];
        }
    });
    return _instance;
}

/*
+(instancetype)defaultManager {return [[self alloc]init];}
+(instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (_instance == nil) {
            _instance = [super allocWithZone:zone];
        }
    });
    return _instance;
}
-(instancetype)copyWithZone:(NSZone *)zone {return _instance;}
-(instancetype)mutableCopyWithZone:(NSZone *)zone {return _instance;}
*/

+ (void)playWithJson:(NSString *)json
              speed:(CGFloat)speed
        repeatCount:(NSInteger)repeatCount
         identifier:(NSString *)identifier
             layout:(void (^)(WXAnimationView *aniView))layout
           autoPlay:(BOOL)autoPlay
         completion:(void (^)(BOOL finished))completion; {
    [AniamtionManager playWithJson:json speed:speed repeatCount:repeatCount identifier:identifier layout:layout autoPlay:autoPlay completion:completion];
}

+ (WXAnimationView *)getAnimationByIdentifier:(NSString *)identifier {
    return [AniamtionManager getAnimationByIdentifier:identifier];
}

+ (void)playAllAnimations {
    [AniamtionManager playAllAnimations];
}
+ (void)playWithIdentifier:(NSString *)identifier {
    [AniamtionManager playWithIdentifier:identifier];
}

+ (void)pauseAllAnimations {
    [AniamtionManager pauseAllAnimations];
}
+ (void)pauseWithIdentifier:(NSString *)identifier {
    [AniamtionManager pauseWithIdentifier:identifier];
}

+ (void)stopAniamtionWithIdentifier:(NSString *)identifier {
    [AniamtionManager stopAniamtionWithIdentifier:identifier];
}
+ (void)stopAllAnimations {
    [AniamtionManager stopAllAnimations];
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForeground:)                                              name:UIApplicationWillEnterForegroundNotification object:nil];
        
    }
    return self;
}

- (void)appWillResignActive:(NSNotification *)note {
    //    for (WXAnimationView *aniView in self.animates) {
    //        [aniView.playView setAnimationProgress:1];
    //    }
}

- (void)willEnterForeground:(NSNotification *)note {
    
}

#pragma mark - 自管理，实例方法
- (void)playWithJson:(NSString *)json
              speed:(CGFloat)speed
        repeatCount:(NSInteger)repeatCount
         identifier:(NSString *)identifier
             layout:(void (^)(WXAnimationView *aniView))layout
           autoPlay:(BOOL)autoPlay
         completion:(void (^)(BOOL finished))completion; {
    // 先布局动画才能正常结束
    if (layout) {
        WXAnimationView *ani = [[WXAnimationView alloc] initWithJson:json speed:speed repeatCount:repeatCount completion:completion];
        ani.identifier = identifier;
        layout(ani);
        [self.animates addObject:ani];
        if (autoPlay) {
            [ani play];
        }
    }
}

- (void)playWithFilePath:(NSString *)filePath
                   speed:(CGFloat)speed
             repeatCount:(NSInteger)repeatCount
              identifier:(NSString *)identifier
                  layout:(void (^)(WXAnimationView *aniView))layout
                autoPlay:(BOOL)autoPlay
              completion:(void (^)(BOOL finished))completion {
    // 先布局动画才能正常结束
    if (layout) {
        WXAnimationView *ani = [[WXAnimationView alloc] initWithFilePath:filePath speed:speed repeatCount:repeatCount completion:completion];
        ani.identifier = identifier;
        layout(ani);
        [self.animates addObject:ani];
        if (autoPlay) {
            [ani play];
        }
    }
}

- (void)playWithJson:(NSString *)json
               speed:(CGFloat)speed
         repeatCount:(NSInteger)repeatCount
          identifier:(NSString *)identifier
        fromProgress:(CGFloat)fromStartProgress
          toProgress:(CGFloat)toEndProgress
              layout:(void (^)(WXAnimationView *aniView))layout
            autoPlay:(BOOL)autoPlay
          completion:(void (^)(BOOL finished))completion{
    
    // 先布局动画才能正常结束
    if (layout) {
        WXAnimationView *ani = [[WXAnimationView alloc] initWithJson:json speed:speed repeatCount:repeatCount completion:completion];
        ani.identifier = identifier;
        layout(ani);
        [self.animates addObject:ani];
        if (autoPlay) {
            [ani playFromProgress:fromStartProgress toProgress:toEndProgress];
        }
    }
}

- (void)playWithJson:(NSString *)json
               speed:(CGFloat)speed
         repeatCount:(NSInteger)repeatCount
          identifier:(NSString *)identifier
           fromFrame:(nonnull NSNumber *)fromStartFrame
             toFrame:(nonnull NSNumber *)toEndFrame
              layout:(void (^)(WXAnimationView *aniView))layout
            autoPlay:(BOOL)autoPlay
          completion:(void (^)(BOOL finished))completion{
    // 先布局动画才能正常结束
    if (layout) {
        WXAnimationView *ani = [[WXAnimationView alloc] initWithJson:json speed:speed repeatCount:repeatCount completion:completion];
        ani.identifier = identifier;
        layout(ani);
        [self.animates addObject:ani];
        if (autoPlay) {
            [ani playFromFrame:fromStartFrame toProgress:toEndFrame];
        }
    }
}
- (WXAnimationView *)getAnimationByIdentifier:(NSString *)identifier {
    for (WXAnimationView *obj in self.animates) {
        if ([obj.identifier isEqualToString:identifier]) {
            return obj;
        }
    }
    return nil;
}

- (void)playAllAnimations {
    for (WXAnimationView *obj in self.animates) {
        [obj play];
    }
}

- (void)playWithIdentifier:(NSString *)identifier {
    for (WXAnimationView *obj in self.animates) {
        if ([obj.identifier isEqualToString:identifier]) {
            [obj play];
            break;
        }
    }
}

- (void)pauseAllAnimations {
    for (WXAnimationView *obj in self.animates) {
        [obj pause];
    }
}

- (void)pauseWithIdentifier:(NSString *)identifier {
    for (WXAnimationView *obj in self.animates) {
        if ([obj.identifier isEqualToString:identifier]) {
            [obj pause];
            break;
        }
    }
}

- (void)stopAniamtionWithIdentifier:(NSString *)identifier {
    NSMutableArray *dataM = [NSMutableArray arrayWithArray:self.animates];
    for (WXAnimationView *obj in dataM) {
        if ([obj.identifier isEqualToString:identifier]) {
            [obj stop];
            [self removeSubViewsFromView:obj];
            [obj removeFromSuperview];
            [dataM removeObject:obj];
            NSLog(@"stopAniamtionWithIdentifier===============%@",identifier);
            break;
        }
    }
    self.animates = [NSMutableArray arrayWithArray:dataM];
}

- (void)stopAllAnimations {
    NSMutableArray *dataM = [NSMutableArray arrayWithArray:self.animates];
    for (WXAnimationView *obj in dataM) {
        [obj stop];
        [self removeSubViewsFromView:obj];
        [obj removeFromSuperview];
        NSLog(@"stopAllAnimations===============%@",obj.animationJson);
    }
    self.animates = [NSMutableArray arrayWithArray:dataM];
    [self.animates removeAllObjects];
}


- (void)removeSubViewsFromView:(UIView *)view {
    for (id sub in view.subviews) {
        if ([sub isKindOfClass:[UIView class]]) {
            [(UIView *)sub removeFromSuperview];
        }
    }
}

#pragma mark - lazyLoad
- (NSMutableArray *)animates {
    if (!_animates) {
        _animates = [NSMutableArray array];
    }
    return _animates;
}

@end
